//get state-level enrollment-weighted average costs

//location and classifications
forval y = 2001 / 2005{
	clear all
	import delimited "$data/IPEDS/location/hd`y'_data_stata.csv", clear
	keep unitid fips sector iclevel control carnegie
	gen year = `y'
	save "$temp/iped_location_`y'", replace
}

//enrollment
forval y = 2001/2005{
	import delimited "$data/IPEDS/enrollment/ef`y'a_data_stata.csv", clear
	keep if line == 8 //full time undergrads
	gen total_enroll = efrace15 + efrace16
	keep unitid total
	gen year = `y'
	save "$temp/iped_enroll_`y'", replace
}

//sfa
forval y = 2001/2005{
	import delimited "$data/IPEDS/sfa/sfa`y'_data_stata.csv", clear
	keep unitid fgrnt_a sgrnt_a igrnt_a loan_a
	gen year = `y'
	save "$temp/iped_sfa_`y'", replace
}

//sat/act, admissions
forval y = 2001/2005{
	import delimited "$data/IPEDS/inst/ic2001_data_stata.csv", clear
	
	if `y'==2001{
		keep unitid satmt25 satmt75 actmt25 actmt75
	}
	else{
		keep unitid satmt25 satmt75 actmt25 actmt75 applcnm applcnw admssnm admssnw
	}
	gen year = `y'
	save "$temp/iped_admission_`y'", replace
}

//instructor salaries
forval y = 2001/2005{
	import delimited "$data/IPEDS/salary/sal`y'_a_data_stata.csv", clear
	
	if `y' == 2001{
		keep if contract == 1
		keep if arank == 1 | arank == 8
		collapse (mean) saverage, by(unitid)
		ren saverage avg_sal
	}
	else{
		keep if contract == 1
		keep if arank == 1
		ren avesalt avg_sal
		keep unitid avg_sal
	}
	
	gen year = `y'
	save "$temp/iped_sal_`y'", replace
}

//number of instructors
forval y = 2001/2005{
	import delimited "$data/IPEDS/staff/s`y'_f_data_stata.csv", clear
	keep if arank==22
	gen total_staff = staff15 + staff16
	keep unitid total
	gen year = `y'
	save "$temp/iped_staff_`y'", replace
}


//student charges
forval y = 2001/2005{
	clear all
	import delimited "$data/IPEDS/charges/ic`y'_ay_data_stata.csv", clear
	
	//tuition, books, room and board
	keep unitid chg2ay3 chg3ay3 chg4ay3 chg5ay3
	ren chg2 tuition_instate
	ren chg3 tuition_outstate
	ren chg4 books
	ren chg5 roomboard
	gen year = `y'
	save "$temp/iped_charges_`y'", replace
}

//add all together
forval y = 2001/2005{
	use "$temp/iped_location_`y'", clear
	merge 1:1 unitid using "$temp/iped_enroll_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_admission_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_sal_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_staff_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_charges_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_sfa_`y'", keep(1 3) nogen
	save "$temp/iped_all_`y'", replace
}

clear
forval y = 2001/2005{
	append using "$temp/iped_all_`y'"
}
save "$temp/iped_all", replace

//define college types and report average costs by types
use "$temp/iped_all", clear
drop if total_enroll == 0 | total_enroll == . //weighting by enrollment anyhow, so basically have to drop these
do "$dofiles/pre_est/99_ipeds_labels"
gen admit_rate = (admssnm + admssnw) / (applcnm + applcnw) 
replace satmt25 = 0 if satmt25==.
replace actmt25 = 0 if actmt25==.

//categories, based on Kennan (2020)
gen cat = .
replace cat = 1 if sector == 4 | sector == 7 //community college (public 2 year or below)
replace cat = 2 if sector==5 | sector == 6 | sector == 8 | sector == 9 //private two-year
replace cat = 3 if sector == 1 //other public universities

//categorize private schools based on selectivity. Want either sat25>=380 or act25>=16
replace cat = 4 if sector == 2 & (satmt25>=380 | actmt25>=16 | admit_rate<0.5)  //non-profit private schools that pass certain criteria
replace cat = 5 if cat == . //other: low-quality private schools
replace cat = 5 if cat == 2
replace cat = cat-1 if cat>1 //collapse categories

//in-state and out-state costs
drop if tuition_instate == . | tuition_outstate == .
gen cost_instate = tuition_instate + books + roomboard
gen cost_outstate = tuition_outstate + books + roomboard




//deflate
merge m:1 year using "$data/GDP/gdp_pce_deflator", keep(match) nogen
replace cost_instate = cost_instate/deflator * 100
replace cost_outstate = cost_outstate/deflator * 100
replace cost_instate = cost_instate * 4 / 40000
replace cost_outstate = cost_outstate * 4 / 40000




replace fgrnt_a = fgrnt_a/deflator * 100
replace sgrnt_a = sgrnt_a/deflator * 100
replace igrnt_a = igrnt_a/deflator * 100
replace loan_a = loan_a/deflator * 100
replace fgrnt_a = fgrnt_a * 4 / 40000
replace sgrnt_a = sgrnt_a * 4 / 40000
replace igrnt_a = igrnt_a * 4 / 40000
replace loan_a = loan_a * 4 / 40000



//switch categories and report
replace cat = 5 if cat == 3
replace cat = 3 if cat == 4
replace cat = 4 if cat == 5

//replace cost_instate = cost_instate - sgrnt_a - igrnt_a
//replace cost_outstate = cost_outstate - sgrnt_a - igrnt_a

su cost* [w = total_enroll] if cat == 1
su cost* [w = total_enroll] if cat == 2
su cost* [w = total_enroll] if cat == 3
su cost* [w = total_enroll] if cat == 4


//export costs by weighting in/out-state attendance decisions in NLSY (0.915, 0.86)
su cost_instate [w = total_enroll] if cat == 1
local r1 = `r(mean)'

su cost_outstate [w = total_enroll] if cat == 1
local r2 = `r(mean)'
local cost_1 = (0.915 * `r1' + (1-0.915) * `r2')


su cost_instate [w = total_enroll] if cat == 2
local r1 = `r(mean)'

su cost_outstate [w = total_enroll] if cat == 2
local r2 = `r(mean)'
local cost_2 = (0.86 * `r1' + (1-0.86) * `r2')

su cost_outstate [w = total_enroll] if cat == 3
local cost_3 = `r(mean)'
su cost_outstate [w = total_enroll] if cat == 4
local cost_4 = `r(mean)'

preserve
clear
set obs 5
gen v1 = .
replace v1 = 0 in 1
forval y = 2/5{
	local i = `y'-1
	replace v1 = `cost_`i'' in `y'
}
export delimited "$model/utilities/educ_costs.csv", replace novarn
restore

save "$temp/ipeds_processed", replace





